// miniprogram/pages/bar/house/house.js
const APIUrl = require('../../../tools/APIurl');
const mAPIUrl = new APIUrl()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTitle: [{
      title: "闲置房源",
      titleId: "1",
      creatPoint: "set_aside"
    }, {
      title: "已租房源",
      titleId: "2",
      creatPoint: "used"
    }],
    chartSelect: 1,
    idx: "1",
    currentPoint: "set_aside",
    itemList: [{
        childLeftTop: '8栋1单元801室',
        address:'',
        childLeftBottom: '一室30.0',
        childRightText: '添加租客',
        events: 'residentsInfo'
      },
      {
        childLeftTop: '8栋1单元802室',
        childLeftBottom: '一室30.0',
        childRightText: '添加租客',
        events: 'houseInfoTap'
      },
      {
        childLeftTop: '8栋1单元803室',
        childLeftBottom: '一室30.0',
        childRightText: '添加租客',
        events: 'houseInfoTap'
      },

    ],
    itemList1: [{
      childLeftTop: '8栋1单元708室',
      address:'',
      childLeftBottom: '一室30.0',
      childRightText: '已出租',
      events: 'residentsInfo'
    },
  ],
  arrayIndex:[],
  array: [],
  index:0,
  address_id:0
  },
  selectApply: function (e) {
    var that = this;
    var id = e.target.dataset.id
    var list = that.data.topTitle
    that.setData({
        idx: id,
        chartSelect: id,
        currentPoint: list[id - 1].creatPoint
      }),
      console.log(that.data.currentPoint)
  },
  houseInfoTap() {
    wx.navigateTo({
      url: '/pages/bar/house/houseInfo/houseInfo?address='+that.data.array[that.data.index]+'&address_id='+that.data.arrayIndex[that.data.index],
    })
  },
  addHouseInfo() {
    wx.navigateTo({
      url: '/pages/bar/house/houseInfo/houseInfo',
    })
  },
  residentsInfo(e){
    console.log(e)
    var arrayIndex=that.data.arrayIndex[that.data.index]
    var index = that.data.array[that.data.index]
    var number = e.currentTarget.dataset.number
    wx.navigateTo({
      url: '/pages/bar/house/residentsInfo/residentsInfo?address_id='+arrayIndex+'&address='+index+'&number='+number+'&id='+e.currentTarget.dataset.id,
    })
  },
  houseGuestInfo(e){
    var arrayIndex=that.data.arrayIndex[that.data.index]
    var index = that.data.array[that.data.index]
    var number = e.currentTarget.dataset.number
    wx.navigateTo({
      url: '/pages/bar/house/houseInfo/houseGuestInfo/houseGuestInfo?address_id='+arrayIndex+'&address='+index+'&number='+number+'&id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    mAPIUrl.list('/api/mgr/address',{
      action: 'list',
      user_id: wx.getStorageSync('userInfo').id
    },function(res){
      console.log(res)
      var array = []
      var arrayIndex=[]
      for(var i = 0;i<res.length;i++){
        array=array.concat(res[i].community+res[i].number)
        arrayIndex=arrayIndex.concat(res[i].id)
      }
      that.setData({
        array:array,
        arrayIndex:arrayIndex
      })
    })
    mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_idle_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:1
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '添加租客',
          events: 'residentsInfo',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       
       that.setData({
        itemList:itemList
       })
    })
    mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_sold_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:1
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '已出租',
          events: 'houseGuestInfo',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       
       that.setData({
        itemList1:itemList
       })
    })
  },
  pickerChange(e){
    var arrayIndex = that.data.arrayIndex
    console.log(arrayIndex[e.detail.value])
    that.setData({
      index:e.detail.value,
      address_id:arrayIndex[e.detail.value]
     })
     mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_idle_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:arrayIndex[e.detail.value]
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '添加租客',
          events: 'residentsInfo',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       
       that.setData({
        itemList:itemList
       })
    })
    mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_sold_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:arrayIndex[e.detail.value]
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '已出租',
          events: '',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       that.setData({
        itemList1:itemList
       })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this
    mAPIUrl.list('/api/mgr/address',{
      action: 'list',
      user_id: wx.getStorageSync('userInfo').id
    },function(res){
      console.log(res)
      var array = []
      var arrayIndex=[]
      for(var i = 0;i<res.length;i++){
        array=array.concat(res[i].community+res[i].number)
        arrayIndex=arrayIndex.concat(res[i].id)
      }
      that.setData({
        array:array,
        arrayIndex:arrayIndex
      })
    })
    mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_idle_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:1
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '添加租客',
          events: 'residentsInfo',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       
       that.setData({
        itemList:itemList
       })
    })
    mAPIUrl.list('/api/mgr/rsAddress', { 
      action: 'lists_sold_house',
      user_id: wx.getStorageSync('userInfo').id,
      address_id:1
    }, 
     function (res) {
       console.log(res)
       var itemList =[]
       for(var i=0;i<res.length;i++){
        var obj = {
          childLeftTop: res[i].Room_number,
          address:'',
          address_id:res[i].address_id,
          childLeftBottom: res[i].doors+res[i].area,
          childRightText: '已出租',
          events: 'houseGuestInfo',
          id:res[i].id
         }
         itemList=itemList.concat(obj)
       }
       
       that.setData({
        itemList1:itemList
       })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})